<template>
  <div class="view-fill">
    <div v-if="!modifyInfo.visible" class="app-container-flex">
      <NewFullScreen
        v-model="showDialog"
        title="应付管理详情"
        @returnBack="returnBack"
      >
        <template v-slot:full-button>
          <el-button v-if="canUpdate" @click="update">修改</el-button>
          <el-button v-if="canPay" @click="toPay">支付</el-button>
          <el-button v-if="canPayCancle" @click="canclePay">取消支付</el-button>
          <el-button v-if="canExport" @click="exportDetailExcel"
            >导出excel</el-button
          >
          <PopoverPrint
            ref="popoverShow"
            v-model:popoverVisible="popoverVisible"
            :canPrint="canPrint"
            :templateArr="templateArr"
            @tempClick="tempClick"
            @print="print"
          ></PopoverPrint>
        </template>
        <div class="model-panel">
          <div class="model-panel-title">
            <p>{{ $t('basic_information') }}</p>
          </div>
          <div class="model-panel-body">
            <basic-information :info="infoList"> </basic-information>
          </div>
        </div>

        <div class="model-panel">
          <div class="model-panel-title">
            <Tooltip :text="$t('detailInfo')"></Tooltip>
          </div>
          <div class="model-panel-body">
            <div class="flex_full_item">
              <!-- 表格 -->
              <page-table
                :refresh="tableInfo.refresh"
                :init-curpage="tableInfo.initCurpage"
                v-model:data="tableInfo.data"
                :api="tableInfo.api"
                :pager="false"
                :check-box="false"
                :tab-index="true"
                :summary="true"
                :summaryFieldList="tableInfo.summaryFieldList"
                :query="filterInfo.query"
                :field-list="tableInfo.fieldList"
                :isDynamic="true"
              >
                <template v-slot:col-returnQty="scope">
                  <span v-if="scope.row.returnQty">
                    {{ scope.row.returnQty + scope.row.preNetworkUnit }}
                  </span>
                  <span v-else>-</span>
                </template>
                <template v-slot:col-receivingQty="scope">
                  <span v-if="scope.row.receivingQty">
                    {{ scope.row.receivingQty + scope.row.preNetworkUnit }}
                  </span>
                  <span v-else>-</span>
                </template>
                <template v-slot:col-deliverQty="scope">
                  <span v-if="scope.row.deliverQty">
                    {{ scope.row.deliverQty + scope.row.preNetworkUnit }}
                  </span>
                  <span v-else>-</span>
                </template>
                <template v-slot:col-preNetworkPrice="scope">
                  <span v-if="scope.row.preNetworkPrice">
                    {{
                      scope.row.preNetworkPrice +
                      '元/' +
                      scope.row.preNetworkUnit
                    }}
                  </span>
                  <span v-else>-</span>
                </template>
              </page-table>
            </div>
          </div>
        </div>
      </NewFullScreen>
    </div>
    <!-- 新增或修改 -->
    <modify
      v-if="modifyInfo.visible"
      v-model:visible="modifyInfo.visible"
      :title="modifyInfo.title"
      :data="modifyInfo.data"
      :orderId="modifyInfo.orderId"
      @returnModify="returnModify"
    />
  </div>
</template>

<script setup name="meetManageOrderDetailView">
import {
  shouldPayOrderDetailAll,
  getShouldPayOrderDetails,
  shouldPayOrderPayCancel,
  shouldPayOrderPay,
  excelShouldPayOrderDetail,
} from '@/api/receipt'
import _ from 'lodash'
import Modify from './modify'
import { doPrint } from '@/utils/exportUtil.js'
import { selectByCodeAndDept } from '@/api/report.js'

const emits = defineEmits()
const { proxy } = getCurrentInstance()
const props = defineProps(['visible', 'orderId', 'data'])
const infoList = ref({
  data: '',
  fieldList: [
    { label: '供应商', value: 'supplierName', type: 'text' },
    { label: '用途', value: 'use', type: 'text' },
    { label: '支票号', value: 'checkNumber', type: 'text' },
    { label: '科目', value: 'account', type: 'text' },
    { label: '状态', value: 'statusValue', type: 'text' },
    { label: '备注', value: 'remark', type: 'text' },
  ],
})
// 过滤相关配置
const filterInfo = ref({
  sortArr: [],
  copyQueryMap: {},
  query: {
    queryMap: {
      orderId: {
        queryType: 'EQUALS',
        queryCategory: 'AND',
        value: null,
      },
    },
    queryParams: {},
  },
  searchList: [],
  searchBtn: [],
  btnList: [],
})

// 表格相关
const tableInfo = ref({
  api: undefined,
  summaryFieldList: [
    { value: 'shouldPayAmount', formatter: proxy.$fn.qtyFormatter },
    { value: 'discountsAmount', formatter: proxy.$fn.qtyFormatter },
    { value: 'payAmount', formatter: proxy.$fn.TripartiteMethod },
  ],
  fieldList: [
    {
      label: '发票号',
      value: 'receiptNumber',
    },
    {
      label: '应付金额',
      value: 'shouldPayAmount',
      formatter: proxy.$fn.qtyFormatter,
    },
    {
      label: '优惠金额',
      value: 'discountsAmount',
    },
    {
      label: '付款金额',
      value: 'payAmount',
      formatter: proxy.$fn.TripartiteMethod,
    },
  ],
  refresh: 1,
  initCurpage: 1,
  data: [],
})

//新增或更新相关
const modifyInfo = ref({
  visible: false,
  title: '',
  orderId: '',
  data: {},
})

onMounted(() => {
  getList()
})

const showDialog = computed({
  get() {
    return props.visible
  },
  set(newValue) {
    emits('update:visible', newValue) // 更新父组件shows
  },
})

const canUpdate = computed(() => {
  return proxy.$permission('meetManage:update') && !infoList.value.data.status
})
const canPay = computed(() => {
  return proxy.$permission('meetManage:pay') && !infoList.value.data.status
})
const canPayCancle = computed(() => {
  return (
    proxy.$permission('meetManage:canclePay') && infoList.value.data.status == 1
  )
})
const canExport = computed(() => {
  return proxy.$permission('meetManage:export')
})
const canPrint = computed(() => {
  return proxy.$permission('meetManage:printDetail')
})

function returnModify() {
  getList()
}
const templateArr = ref([])
const popoverVisible = ref(false)
//打印
function print() {
  selectByCodeAndDept({
    tempCode: 'shouldPayOrderDetail',
  }).then((res) => {
    if (res && res.success) {
      templateArr.value = res.data
      popoverVisible.value = true
      // 一条数据则直接打印
      if (templateArr.value.length === 1) {
        proxy.$refs.popoverShow.hidePopover()
        doPrint({
          name: templateArr.value[0].ureportName,
          params: {
            applicationId: infoList.value.data.id,
            orderId: infoList.value.data.id,
            orderNo: infoList.value.data.orderNo,
          },
          callback: printCallback,
          data: {},
        })
      }
    }
  })
}
function tempClick(item) {
  doPrint({
    name: item.ureportName,
    params: {
      applicationId: infoList.value.data.id,
      orderId: infoList.value.data.id,
      orderNo: infoList.value.data.orderNo,
    },
    callback: printCallback,
    data: {},
  })
  popoverVisible.value = false
}
function printCallback() {}
function update() {
  sessionStorage.setItem('storeRowId', infoList.value.data.id)
  modifyInfo.value.visible = true
  modifyInfo.value.title = '修改应付单'
  // 显示信息
  modifyInfo.value.orderId = infoList.value.data.id
  modifyInfo.value.data = infoList.value.data
}

function returnBack() {
  emits('returnBack', false)
}

function toPay() {
  const ids = []
  ids.push(infoList.value.data.id)
  shouldPayOrderPay(ids).then((resp) => {
    if (resp && resp.success) {
      getList()
    }
  })
}
function canclePay() {
  const ids = []
  ids.push(infoList.value.data.id)
  shouldPayOrderPayCancel(ids).then((resp) => {
    if (resp && resp.success) {
      getList()
    }
  })
}

function exportDetailExcel() {
  excelShouldPayOrderDetail(infoList.value.data.id).then((resp) => {
    if (resp) {
      proxy.$fn.parseExcel(resp)
    }
  })
}

function getList() {
  shouldPayOrderDetailAll({ orderId: props.orderId }).then((resp) => {
    if (resp && resp.success) {
      tableInfo.value.data = resp.data
    }
  })
  getShouldPayOrderDetails(props.orderId).then((resp) => {
    if (resp && resp.success) {
      infoList.value.data = resp.data
    }
  })
}
</script>
<style scoped lang="scss">
:deep(.page-table) > .el-table {
  overflow-y: hidden;
}
.model-panel-title {
  display: flex;
  align-items: center;
  .model-tips {
    display: flex;
    align-items: center;
    margin-left: 10px;
    color: #909399;
    .model-tips-nearly,
    .model-tips-expire {
      display: block;
      margin-left: 5px;
      width: 17px;
      height: 17px;
    }
    .model-tips-nearly {
      background: #fceacb;
    }
    .model-tips-expire {
      background: #f5e4e4;
    }
  }
}
:deep(.el-table) {
  .nearly-row {
    background: #fceacb;
  }
  .expire-row {
    background: #f5e4e4;
  }
}
</style>
